package com.controller;

/**
 * Created by wlc on 2018/5/16.
 */

import com.service.NeuralNetworks.NNModelConstruction;
import com.service.NeuralNetworks.NNModelOperator;
import com.service.NeuralNetworks.NNParameterSettings;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * 映射神经网络构建部分
 */
@Controller
@RequestMapping(value = "/NeuralNetworks",produces = "text/html;charset=UTF-8")
public class NeuralNetworksController {
    //映射到神经网络新建
    @RequestMapping(value = "/newNeuralNetworks",produces = "text/html;charset=UTF-8")
    public void newNeuralNetworks(HttpServletResponse response)throws IOException{
        String feedBackMessage = NNModelConstruction.NeuralNetworksModel();
        PrintWriter out= response.getWriter();
        out.print(feedBackMessage);
        out.flush();
        out.close();
    }

    //映射到神经元添加
    @RequestMapping(value = "/NeuronsAdd",produces = "text/html;charset=UTF-8")
    public void NeuronsAdd(HttpServletResponse response,
                           @RequestParam(value = "NeuralNetworksName")String NeuralNetworksName,
                           @RequestParam(value = "neuronsId")String neuronsId)throws IOException{
        String feedBackMessage = NNModelConstruction.NeuronsModel(NeuralNetworksName,neuronsId);
        PrintWriter out= response.getWriter();
        out.print(feedBackMessage);
        out.flush();
        out.close();
    }

    //映射到突触添加
    @RequestMapping(value = "/SynapseAdd",produces = "text/html;charset=UTF-8")
    public void SynapseAdd(HttpServletResponse response,
                           @RequestParam(value = "NeuralNetworksName")String NeuralNetworksName,
                           @RequestParam(value = "synapseId")String synapseId)throws IOException{
        String feedBackMessage = NNModelConstruction.SynapseModel(NeuralNetworksName,synapseId);
        PrintWriter out= response.getWriter();
        out.print(feedBackMessage);
        out.flush();
        out.close();
    }

    //映射到激活函数添加
    @RequestMapping(value = "/ActivationAdd",produces = "text/html;charset=UTF-8")
    public void ActivationAdd(HttpServletResponse response,
                              @RequestParam(value = "NeuralNetworksName")String NeuralNetworksName,
                              @RequestParam(value = "neuronsId")String neuronsId,
                              @RequestParam(value = "functionId")String functionId)throws IOException{
        String feedBackMessage = NNParameterSettings.ActivationFunction(NeuralNetworksName,neuronsId,functionId);
        PrintWriter out= response.getWriter();
        out.print(feedBackMessage);
        out.flush();
        out.close();
    }

    //映射到神经网络模型的训练
    @RequestMapping(value = "/run",produces = "text/html;charset=UTF-8")
    public void run(HttpServletResponse response,
                    @RequestParam(value = "NeuralNetworksName")String NeuralNetworksName)throws IOException{
        String feedBackMessage = NNModelOperator.train(NeuralNetworksName);
        PrintWriter out= response.getWriter();
        out.print(feedBackMessage);
        out.flush();
        out.close();
    }
}
